package fdoj.eventInvitations.entity;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.EAGER;
import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;



@Entity
@Table(name="persons")
public class Person implements Serializable{
	/**
	 *
	 */
	private static final long serialVersionUID = 8149756573288993067L;

	@Id
	@GeneratedValue(strategy=IDENTITY)
	@Column(name="person_id", unique=true, nullable=false)
	private Integer id;
	
	@Column(name="person_firstname", unique=false, nullable=false)
	private String firstName;

	@Column(name="person_lastname", unique=false, nullable=false)
	private String lastName;

	@Column(name="person_phoneNum", unique=false, nullable=true)
	private String phoneNum;

	@Column(name="person_email", unique=true, nullable=false)
	private String email;

	@Column(name="regToken", unique=false, nullable=false)
	private String regToken;// prilikom slanja mejla za potvrdu

	@Column(name="person_password", unique=false, nullable=false)
	private String password;

	@OneToMany(cascade={ALL}, fetch=EAGER, mappedBy="addressBook")
	private Set<Contact> contacts = new HashSet<Contact>();

	@OneToMany(cascade={ALL}, fetch=EAGER, mappedBy="owner")
	private Set<Event> events = new HashSet<Event>();

	@OneToMany(cascade={ALL}, fetch=EAGER, mappedBy="poster")
	private Set<Comment> posts = new HashSet<Comment>();
	
	public void addContact(Contact contact) {
	    if (contact.getAddressBook() != null)
	    	contact.getAddressBook().getContacts().remove(contact);
	    contact.setAddressBook(this);
	    contacts.add(contact);
	  }
	
	public void removeContact(Contact contact){
		contact.setAddressBook(null);
		contacts.remove(contact);
	}
	public void addEvent(Event event) {
	    if (event.getOwner() != null)
	    	event.getOwner().getContacts().remove(event);
	    event.setOwner(this);
	    events.add(event);
	  }
	
	public void removeEvent(Event event){
		event.setOwner(null);
		events.remove(event);
	}
	public void addComment(Comment comment) {
	    if (comment.getPoster() != null)
	    	comment.getPoster().getContacts().remove(comment);
	    comment.setPoster(this);
	    posts.add(comment);
	  }
	
	public void removeComment(Comment comment){
		comment.setPoster(null);
		posts.remove(comment);
	}
	
	public Person(){

	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRegToken() {
		return regToken;
	}

	public void setRegToken(String regToken) {
		this.regToken = regToken;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set<Contact> getContacts() {
		return contacts;
	}

	public void setContacts(Set<Contact> contacts) {
		this.contacts = contacts;
	}

	public Set<Event> getEvents() {
		return events;
	}

	public void setEvents(Set<Event> events) {
		this.events = events;
	}

	public Set<Comment> getPosts() {
		return posts;
	}

	public void setPosts(Set<Comment> posts) {
		this.posts = posts;
	}
}
